﻿using NLog;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using NLog.Config;
using NLog.LayoutRenderers;

namespace LogCommon
{
    public static class LogHelper
    {
        private static NLog.Logger _loger = null;
        /// <summary>
        /// 程序集名称
        /// </summary>
        public static string AssemblyName { get; set; }

        static LogHelper()
        {
            //注册自定义Layout
            ConfigurationItemFactory.Default.LayoutRenderers
                .RegisterDefinition("CustomLayout", typeof(CustomLayoutLayoutRenderer));
        }

        /// <summary>
        /// 设置线程会话活动Id
        /// </summary>
        public static void SetActivityId(string activityId = null)
        {
            Trace.CorrelationManager.ActivityId = string.IsNullOrEmpty(activityId) ? Guid.NewGuid() : new Guid(activityId);
        }

        private static NLog.Logger GetCurProLoger(string moduleName)
        {
            SetActivityId();
            return _loger = NLog.LogManager.GetLogger(moduleName);
        }

        /// <summary>
        /// 兼容以前的老版本日志打印系统（Fatal:代码异常）
        /// </summary>
        /// <param name="ModuleName">自定义日志文件名称</param>
        /// <param name="message">错误信息</param>
        public static void Write(string moduleName, object message)
        {
            _loger = GetCurProLoger(moduleName);
            AssemblyName assemblyName = Assembly.GetCallingAssembly().GetName();
            message = "【程序集名称：" + assemblyName.Name + "】,【版本号：" + assemblyName.Version + "】。\r\n【信息：" + (string)message + "】";
            _loger.Fatal(message);
        }

        /// <summary>
        /// 兼容以前的老版本日志打印系统（Fatal:代码异常）
        /// </summary>
        /// <param name="msg"></param>
        public static void Write(object message)
        {
            AssemblyName assemblyName = Assembly.GetCallingAssembly().GetName();
            _loger = GetCurProLoger(assemblyName.Name);
            message = "【程序集名称：" + assemblyName.Name + "】,【版本号：" + assemblyName.Version + "】。\r\n【信息：" + (string)message + "】";
            _loger.Fatal(message);
        }


        /// <summary>
        /// 调试跟踪信息日志
        /// </summary>
        /// <param name="moduleName">自定义日志文件名称</param>
        /// <param name="message">错误信息</param>
        public static void Info(string moduleName, object message)
        {
            _loger = GetCurProLoger(moduleName);
            AssemblyName assemblyName = Assembly.GetCallingAssembly().GetName();
            message = "【程序集名称：" + assemblyName.Name + "】,【版本号：" + assemblyName.Version + "】。\r\n【信息：" + (string)message + "】";
            _loger.Info(message.ToString());
        }

        /// <summary>
        /// 调试跟踪信息日志
        /// </summary>
        /// <param name="message">错误信息</param>
        public static void Info(object message)
        {
            AssemblyName assemblyName = Assembly.GetCallingAssembly().GetName();
            _loger = GetCurProLoger(assemblyName.Name);
            message = "【程序集名称：" + assemblyName.Name + "】,【版本号：" + assemblyName.Version + "】。\r\n【信息：" + (string)message + "】";
            _loger.Info(message.ToString());
        }

        /// <summary>
        /// 错误日志
        /// </summary>
        /// <param name="moduleName">自定义日志文件名称</param>
        /// <param name="message">错误信息</param>
        public static void Error(string moduleName, object message)
        {
            _loger = GetCurProLoger(moduleName);
            AssemblyName assemblyName = Assembly.GetCallingAssembly().GetName();
            message = "【程序集名称：" + assemblyName.Name + "】,【版本号：" + assemblyName.Version + "】。\r\n【信息：" + (string)message + "】";
            _loger.Error(message.ToString());
        }

        /// <summary>
        /// 业务逻辑错误日志
        /// </summary>
        /// <param name="message">错误信息</param>
        public static void Error(object message)
        {
            AssemblyName assemblyName = Assembly.GetCallingAssembly().GetName();
            _loger = GetCurProLoger(assemblyName.Name);
            message = "【程序集名称：" + assemblyName.Name + "】,【版本号：" + assemblyName.Version + "】。\r\n【信息：" + (string)message + "】";
            _loger.Error(message.ToString());
        }


        /// <summary>
        /// 致命日志
        /// </summary>
        /// <param name="moduleName">自定义日志文件按名称</param>
        /// <param name="message">错误信息</param>
        public static void Fatal(string moduleName, object message)
        {
            _loger = GetCurProLoger(moduleName);
            AssemblyName assemblyName = Assembly.GetCallingAssembly().GetName();
            message = "【程序集名称：" + assemblyName.Name + "】,【版本号：" + assemblyName.Version + "】。\r\n【信息：" + (string)message + "】";
            _loger.Fatal(message);
        }

        /// <summary>
        /// 致命日志
        /// </summary>
        /// <param name="message">错误信息</param>
        public static void Fatal(object message)
        {
            AssemblyName assemblyName = Assembly.GetCallingAssembly().GetName();
            _loger = GetCurProLoger(assemblyName.Name);
            message = "【程序集名称：" + assemblyName.Name + "】,【版本号：" + assemblyName.Version + "】。\r\n【信息：" + (string)message + "】";
            _loger.Fatal(message);
        }

    }

    /// <summary>
    /// 自定义Layout
    /// </summary>
    [LayoutRenderer("CustomLayout")]
    public class CustomLayoutLayoutRenderer : LayoutRenderer
    {
        protected override void Append(StringBuilder builder, LogEventInfo logEvent)
        {
            builder.Append(LogHelper.AssemblyName);
        }
    }
}
